<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use DB;
class TestController extends Controller{

	public function test(){
/*		$arr = [[1,2,3],[4,5,6],[8,9,8]];
		print_r(array_collapse($arr));
		echo '<br>';
		$str = '中华人民共和国成立了!!!';
		print_r(str_limit($str,4)).'<br>';
		echo(str_random()).'<br>';
		echo e('<html>foo</html>').'<br>';
		echo app_path().'<br>';
		echo base_path().'<br>';
		echo config_path().'<br>';*/
		echo public_path().'<br>';
		// echo url('test/add').'<br>';
		// echo bcrypt(123456).'<br>';
		// echo config('app.timezone').'<br>';
		/*$arr = ['ti'=>'13','de'=>'sldak','user'=>['1','3','55']];
		return view('test',$arr);*/
	/*	$data = ['title'=>'天气预报','content'=>'今天天气真不错','score'=>mt_rand(40,90),'user'=>['张三','李四','王五']];
		return view('test',$data);*/
		// $arr = ['user'=>[1,2,3,4,5,],'body'=>[2,3,4,5,6]];
		// $data = ['title'=>'数据库迁移','content'=>'学习Laravel'];
		// return view('up',['score'=>mt_rand(1,100),'title'=>'数据库迁移','content'=>'学习Laravel','code'=>'<script>alert("dang!!!")</script>'],$arr);

		// return view('test',['score'=>mt_rand(1,100)],$arr,$data);
		
	}

	public function add(){
		// echo 'Hello World';
		//插入单行,一维数组形式,数组的键就是表的字段,返回值为true和false
/*		$row = ['name'=>'张三','age'=>23,'email'=>'test@QQ.com'];
		$rs = DB::table('goods')->insert($row);
		return $rs ? 'ok' : 'fail';*/
		//插入多行(多维数组)
		/*$data = array(
			array('name'=>'李四','age'=>35,'email'=>'lisi@qq.com'),
			array('name'=>'王五','age'=>25,'email'=>'wangwu@qq.com')
			);
		$rs = DB::table('goods')->insert($data);
		return $rs ? 'ok' : 'fail';*/
		//插入后返回主键值 获取主键值,用insertGetId()方法,(不允许多维数组)
		$row = array('name'=>'马六','age'=>25,'email'=>'maliu@qq.com');
		$rs = DB::table('goods')->insertGetId($row);
		return $rs ? 'ok' : 'fail';

	}

	public function up(){//典型修改相当于sql:update goods set ('name'='马六','age'=34,'email'='maliu@qq.com') where 'id'= 1;
	/*	$rs = DB::table('goods')->where('id',4)->update(['name'=>'马六','age'=>34,'email'=>'maliu@qq.com']);
		return $rs ? 'ok' : 'fail';*/
		//某字段在原基础上 增长或减少 increment/decrement 返回值是受影响的行数
		/*$rs = DB::table('goods')->where('id',5)->increment('age');
		return $rs;*/
		/*$rs = DB::table('goods')->where('id',6)->increment('age',5);
		return $rs;*/
	/*	$re = DB::table('goods')->where('id',7)->decrement('age');
		return $re;*/
		$re = DB::table('goods')->where('id',8)->decrement('age',5);
		return $re;
	}

	public function del(){
		//where有三个参数时,其中第二个参数当做运算符,返回值是影响的行数
		/*$rs = DB::table('goods')->where('id',9)->delete();
		return $rs;*/
/*		$rs = DB::table('goods')->where('id','>',15)->delete();
		return $rs;*/
		$rs = DB::table('goods')->where('id','>=',13)->delete();
		return $rs;
	}

	public function show(){
		// 注意:取出的数据,无论是单行还是多行,每一行数据都是以一个对象的形式组织的不是关联数组
		//select * from goods;
		/*$rs = DB::table('goods')->get();
		echo '<pre>';
		print_r($rs);*/
		//select * from goods where id>6
		/*$rs = DB::table('goods')->where('id','>',6)->get();
		echo '<pre>';
		print_r($rs);*/
		//select id,email from goods where id>6
		/*$rs = DB::table('goods')->select('id','email')->where('id','>',6)->get();
		echo '<pre>';
		print_r($rs);*/
		//select * from goods where id=6 取出单行,返回
		$rs = DB::table('goods')->where('id',6)->first();
		echo '<pre>';
		print_r($rs);
	}
}
